package com.younion.task;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.younion.business.IMessageService;
import com.younion.common.constants.MsgStatus;
import com.younion.vo.MessageVO;

public class IphonePushTask extends Task {

	private static Logger logger = LoggerFactory
			.getLogger(IphonePushTask.class);
	private IMessageService messageService = null;

	public void setMessageService(IMessageService messageService) {
		this.messageService = messageService;
	}

	@Override
	public void work() {
		Map paramMap = new HashMap();
		paramMap.put("status", MsgStatus.UNSEND.getName());
		paramMap.put("ltsendTime", new Date());
		try {
			List<MessageVO> messageList = messageService.getByMap(paramMap);
			logger.info(".....have "+messageList.size()+" messages to send....." );
			for (MessageVO messageVO : messageList) {
				if (StringUtils.isNotEmpty(messageVO.getDeviceToken())) {
					try{
						messageService.iphonePush(messageVO.getContent(),messageVO.getDeviceToken());
						logger.info("push end...begin save messageVO");
						messageVO.setStatus(MsgStatus.SENDED.getName());
						messageService.save(messageVO);
						logger.info("save success...........");
					}catch(Exception e){
						continue;
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
